package com.examiner.service.impl;

import com.examiner.bean.CommonPerson;
import com.examiner.bean.Department;
import com.examiner.bean.Job;
import com.examiner.bean.test.*;
import com.examiner.bean.vo.MyTestAndTestDBVo;
import com.examiner.bean.vo.NewTestDB;
import com.examiner.bean.vo.NewTestType;
import com.examiner.dao.*;
import com.examiner.page.QueryInfo;
import com.examiner.service.SelectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SelectTestServiceImpl implements SelectService {

    @Resource
    private NewTestUserDao newTestUserDao;

    @Resource
    private TestDBDao testDBDao;

    @Resource
    private NewTestDao newTestDao;

    @Resource
    private TestTypeDao testTypeDao;

    @Resource
    private NewTestItemDao newTestItemDao;

    @Resource
    private CommonPersonDao commonPersonDao;

    @Resource
    private DepartmentDao departmentDao;

    @Resource
    private JobDao jobDao;


    /**
     * 获取考评信息
     * @param nid
     * @return
     */
    @Override
    public NewTestDB getMyTestInfo(Integer nid) throws Exception {
        NewTestType newTestType;
        List<NewTestType> newTestTypes = new ArrayList<>();
        NewTestDB newTestDB = new NewTestDB();
        NewTestUser newTestUser = newTestUserDao.getOne(nid);
        TestDB testDB = testDBDao.getOne(newTestUser.getTid());
        List<TestType> testTypes = testTypeDao.findBySid(testDB.getTestId());
        if (testTypes.size() != 0){
            for (TestType testType : testTypes) {
                newTestType = new NewTestType();
                BeanUtils.copyProperties(testType,newTestType);
                List<NewTestItem> newTestItems =  newTestItemDao.findByNtuidAndSid(newTestUser.getId(),testType.getTestTypeId());
                if (newTestItems.size() != 0){
                    newTestType.setNewTestItems(newTestItems);
                }
                newTestTypes.add(newTestType);
            }
        }
        BeanUtils.copyProperties(testDB,newTestDB);
        newTestDB.setTestTypes(newTestTypes);
        return newTestDB;
    }

    @Override
    public List<MyTestAndTestDBVo> getSelectTestList(Integer uid, QueryInfo queryInfo) throws Exception {
        CommonPerson commonPerson = commonPersonDao.getOne(uid);
        if (commonPerson.getStype().equals("部门")){
            return null;
        }
        List<MyTestAndTestDBVo> list = new ArrayList<>();
        MyTestAndTestDBVo myTestAndTestDBVo;
        List<NewTestUser> myTestList;
        Job job = jobDao.getOne(commonPerson.getSId());
        // 获取当前职位和其所有下级职位
        List<Job> allLowerJob = new ArrayList<>();
        List<Integer> allLowerJobId = new ArrayList<>();
        List<Integer> allLowerDepartmentId = new ArrayList<>();
        // 获取当前职位的所有下级部门
        List<Department> allLowerDepartment = new ArrayList<>();
        allLowerJob.add(job);
        findAllLowerJob(allLowerJob, allLowerDepartment, job);
        if (allLowerJob.size() > 0){
            allLowerJob.forEach(item -> allLowerJobId.add(item.getId()));
        }else {
            allLowerJobId.add(0);
        }
        if (allLowerDepartment.size() > 0 ){
            allLowerDepartment.forEach( item -> allLowerDepartmentId.add(item.getId()));
        }else {
            allLowerDepartmentId.add(0);
        }
        // 根据条件查询 某职位下  的 考评记录
        List<Integer> jobByQuery = jobDao.findIdByQuery(queryInfo.getQuery());
        //jobByQuery = jobByQuery.size() <= 0 ? null:jobByQuery;
        if (jobByQuery.size()<=0){
            jobByQuery.add(0);
        }
        // 根据条件查询 某部门下  的 考评记录
        List<Integer> departmentByQuery = departmentDao.findLikeName(queryInfo.getQuery());
        //departmentByQuery = departmentByQuery.size() <= 0 ? null:departmentByQuery;
        if (departmentByQuery.size()<=0){
            departmentByQuery.add(0);
        }
        // 根据条件查询 某考评标题 的 考评记录
        List<Integer> testDBByQuery = testDBDao.findSelectByTitle(queryInfo.getQuery());
        //testDBByQuery = testDBByQuery.size() <= 0 ? null:testDBByQuery;
        if (testDBByQuery.size()<=0){
            testDBByQuery.add(0);
        }
        // 根据条件查询具体人员 的 考评记录
        List<Integer> personByQuery = commonPersonDao.findIdByQuery(queryInfo.getQuery());
        //personByQuery = personByQuery.size() <= 0 ? null:personByQuery;
        if (personByQuery.size()<=0){
            personByQuery.add(0);
        }
        myTestList = newTestUserDao.findSelectList((queryInfo.getPageNum()-1)* queryInfo.getPageSize(), queryInfo.getPageSize(), allLowerJobId, allLowerDepartmentId ,
                                                    jobByQuery, departmentByQuery, testDBByQuery, personByQuery);
        if (myTestList.size() != 0){
            for (NewTestUser newTestUser : myTestList) {
                myTestAndTestDBVo = new MyTestAndTestDBVo();
                TestDB one = testDBDao.getOne(newTestUser.getTid());
                NewTest one1 = newTestDao.getOne(newTestUser.getNid());
                myTestAndTestDBVo.setTestDB(one);
                myTestAndTestDBVo.setNewTestUser(newTestUser);
                myTestAndTestDBVo.setNewTest(one1);
                list.add(myTestAndTestDBVo);
            }
        }
        return list;
    }

    /**
     * 查询所有下级职位
     * @param jobs
     * @param job
     */
    private void findAllLowerJob(List<Job> jobs, List<Department> departments, Job job){
        List<Job> jobList = jobDao.findBySidAndStype(job.getId(), job.getType());
        List<Department> departmentList = departmentDao.findBySIdAndStype(job.getId(), job.getType());
        if (departmentList.size() > 0){
            departments.addAll(departmentList);
        }
        if (jobList.size() > 0 ){
            jobs.addAll(jobList);
            jobList.forEach( item -> findAllLowerJob(jobs, departments, item));
        }
    }

    /**
     * 获取 查询数据列表的总条数
     * @param uid
     * @param queryInfo
     * @return
     */
    @Override
    public int getSelectTestListTotal(Integer uid, QueryInfo queryInfo) throws Exception{
        CommonPerson commonPerson = commonPersonDao.getOne(uid);
        if (commonPerson.getStype().equals("部门")){
            return 0;
        }
        Job job = jobDao.getOne(commonPerson.getSId());
        List<Integer> allLowerJobId = new ArrayList<>();
        List<Integer> allLowerDepartmentId = new ArrayList<>();
        // 获取当前职位和其所有下级职位
        List<Job> allLowerJob = new ArrayList<>();
        // 获取当前职位的所有下级部门
        List<Department> allLowerDepartment = new ArrayList<>();
        allLowerJob.add(job);
        findAllLowerJob(allLowerJob, allLowerDepartment, job);
        if (allLowerJob.size() > 0){
            allLowerJob.forEach(item -> allLowerJobId.add(item.getId()));
        }else {
            allLowerJobId.add(0);
        }
        if (allLowerDepartment.size() > 0 ){
            allLowerDepartment.forEach( item -> allLowerDepartmentId.add(item.getId()));
        }else {
            allLowerDepartmentId.add(0);
        }
        // 根据条件查询 某职位下  的 考评记录
        List<Integer> jobByQuery = jobDao.findIdByQuery(queryInfo.getQuery());
        //jobByQuery = jobByQuery.size() <= 0 ? null:jobByQuery;
        if (jobByQuery.size()<=0){
            jobByQuery.add(0);
        }
        // 根据条件查询 某部门下  的 考评记录
        List<Integer> departmentByQuery = departmentDao.findLikeName(queryInfo.getQuery());
        //departmentByQuery = departmentByQuery.size() <= 0 ? null:departmentByQuery;
        if (departmentByQuery.size()<=0){
            departmentByQuery.add(0);
        }
        // 根据条件查询 某考评标题 的 考评记录
        List<Integer> testDBByQuery = testDBDao.findSelectByTitle(queryInfo.getQuery());
        //testDBByQuery = testDBByQuery.size() <= 0 ? null:testDBByQuery;
        if (testDBByQuery.size()<=0){
            testDBByQuery.add(0);
        }
        // 根据条件查询具体人员 的 考评记录
        List<Integer> personByQuery = commonPersonDao.findIdByQuery(queryInfo.getQuery());
        //personByQuery = personByQuery.size() <= 0 ? null:personByQuery;
        if (personByQuery.size()<=0){
            personByQuery.add(0);
        }
        int total;
        total =  newTestUserDao.findSelectListTotal(allLowerJobId, allLowerDepartmentId, jobByQuery, departmentByQuery, testDBByQuery, personByQuery);
        return total;
    }


}
